<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Building the MongoDB PHP Driver from source</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongodb.installation.windows.html">« Installing the MongoDB PHP Driver on Windows</a></li>
      <li style="float: right;"><a href="mongodb.configuration.html">运行时配置 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongodb.installation.html">安装</a></li>
    <li>Building the MongoDB PHP Driver from source</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb.installation.manual" class="section">
  <h2 class="title">Building the MongoDB PHP Driver from source</h2>

  <p class="para">
   For driver developers and people interested in the latest bugfixes, you can
   compile the driver from the latest source code on
   <a href="https://github.com/mongodb/mongo-php-driver" class="link external">&raquo;&nbsp;Github</a>. Run the following
   commands to clone and build the project:
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ git clone https://github.com/mongodb/mongo-php-driver.git
$ cd mongo-php-driver
$ git submodule update --init
$ phpize
$ ./configure
$ make all
$ sudo make install</pre>
</div>
   </div>

  </p>

  <p class="para">
   If your system has multiple version of PHP installed (e.g. macOS default,
   Homebrew, <a href="https://www.apachefriends.org/" class="link external">&raquo;&nbsp;XAMPP</a>), note that each
   version of PHP has its own <a href="install.pecl.phpize.html" class="link">phpize</a>
   command and <var class="filename">php.ini</var> file(s). Additionally, each PHP environments (e.g.
   CLI, web) may use separate <var class="filename">php.ini</var> files.
  </p>

  <p class="para">
   By default, the driver will use bundled versions of
   <a href="https://github.com/mongodb/mongo-c-driver/tree/master/src/libbson" class="link external">&raquo;&nbsp;libbson</a>,
   <a href="https://github.com/mongodb/mongo-c-driver" class="link external">&raquo;&nbsp;libmongoc</a>, and
   <a href="https://github.com/mongodb/libmongocrypt" class="link external">&raquo;&nbsp;libmongocrypt</a> and
   attempt to configure them on its own. If these libraries are already
   installed as system libraries, you can instruct the driver to utilize them by
   specifying <code class="literal">--with-libbson=yes --with--libmongoc=yes</code> as
   arguments to <code class="literal">configure</code>. Starting with version 1.7.0 of the
   extension, these arguments are deprecated and you should use
   <code class="literal">--with-mongodb-system-libs=yes</code> instead.
  </p>

  <p class="para">
   For a complete list of <code class="literal">configure</code> options, run
   <strong class="command">configure --help</strong>.
  </p>

  <p class="para">
   When using bundled versions of libbson and libmongoc, the driver will also
   attempt to select an SSL library according to the
   <code class="literal">--with-mongodb-ssl</code> option for
   <code class="literal">configure</code>. The default value is
   <code class="literal">--with-mongodb-ssl=auto</code>, which will search for Secure
   Transport (macOS only), OpenSSL, and LibreSSL, in that order. Additionally,
   you may specify <code class="literal">openssl</code>, <code class="literal">libressl</code>, or
   <code class="literal">darwin</code> to force selection of a particular library,
   respectively.
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    If the build process fails to find an SSL library, check that the
    development packages (e.g. <code class="literal">libssl-dev</code>) and
    <a href="https://en.wikipedia.org/wiki/Pkg-config" class="link external">&raquo;&nbsp;pkg-config</a> are both
    installed.
   </p>

   <p class="para">
    When using Homebrew on macOS, it is common for a system to have multiple
    versions of OpenSSL installed. To ensure that the desired version of OpenSSL
    is selected, the <code class="literal">PKG_CONFIG_PATH</code> environment variable may
    be used to control the search path for <code class="literal">pkg-config</code>. If
    <code class="literal">pkg-config</code> is not used, <code class="literal">configure</code> also
    supports a <code class="literal">--with-openssl-dir=DIR</code> argument, which can be
    used to specify a manual search path (for OpenSSL only).
   </p>
  </p></blockquote>

  <p class="para">
   The final build step, <strong class="command">make install</strong>, will report where
   <var class="filename">mongodb.so</var> has been installed, similar to:
   <div class="example-contents">
<div class="txtcode"><pre class="txtcode">Installing shared extensions:     /usr/lib/php/extensions/debug-non-zts-20151012/</pre>
</div>
   </div>

  </p>

  <p class="para">
   Ensure that the <a href="ini.core.html#ini.extension-dir" class="link">extension_dir</a> option
   in <var class="filename">php.ini</var> points to the directory where <var class="filename">mongodb.so</var>
   was installed. You can query for the option by running:
   <div class="example-contents">
<div class="shellcode"><pre class="shellcode">$ php -i | grep extension_dir
  extension_dir =&gt; /usr/lib/php/extensions/debug-non-zts-20151012 =&gt;
                   /usr/lib/php/extensions/debug-non-zts-20151012</pre>
</div>
   </div>

  </p>

  <p class="para">
   If the directories differ, either change
   <a href="ini.core.html#ini.extension-dir" class="link">extension_dir</a> in <var class="filename">php.ini</var> or
   manually move <var class="filename">mongodb.so</var> to the correct directory.
  </p>

  <p class="para">
   Finally, add the following line to the <var class="filename">php.ini</var> file for each environment
   in which you intend to use the driver:
   <div class="example-contents">
<div class="inicode"><pre class="inicode">extension=mongodb.so</pre>
</div>
   </div>

  </p>
 </div></div></div></body></html>